/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.struts.utils;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.service.services.views.ViewGenerator;
import opiam.admin.faare.struts.managers.AppliParametersManager;

import org.apache.log4j.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;


/**
 * Class which initializes an application.
 *
 */
public class InitializationServlet extends HttpServlet
{
    /**
     * @see javax.servlet.Servlet#init(ServletConfig).
     * Loads configuration and services and loads logical views names.
     */
    @Override
	public void init(ServletConfig arg0) throws ServletException
    {
        super.init(arg0);

        Logger logger = Logger.getLogger(InitializationServlet.class);

        try
        {
            PropertiesManager.getInstance();
            AppliParametersManager.initialize();
            this.getServletContext().setAttribute("logicalViewNames",
                ViewGenerator.getViewNamesList());
            this.getServletContext().setAttribute("init_success", "true");
        }
        catch (RuntimeException e)
        {
            this.getServletContext().removeAttribute("init_success");
            logger.error("****************************************************");
            logger.error(
                " INITIALIZATIONSERVLET FAILED. PLEASE REBOOT THE WEBAPP !");
            logger.error("****************************************************");
            throw new ServletException("InitializationServlet failed");
        }

        logger.info("******************************************");
        logger.info(" INITIALIZATIONSERVLET ENDED SUCCESSFULLY");
        logger.info("******************************************");
    }
}
